//
//  DYPrintTaskCmd.m
//  DYPrintSDK
//
//  Created by mr.ye on 2023/11/22.
//

#import "DYPrintTaskCmd.h"
#import "DYPrintParse.h"


@implementation DYPrintTaskCmd


/// 状态查询
/// - Parameters:
///   - compelete: 回调
+ (instancetype)getStateTaskCompelete:(TaskCompelete)compelete {
    
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd stateCmd];
    task.parse =  ^NSObject *(NSData *data) {
        
        UInt8 statusByte;
        [data getBytes:&statusByte length:1];
        NSUInteger state = [DYPrintParse parsePrinterStatus:statusByte];
        return @(state);
    };
    task.compelete = compelete;
    return task;
}
//基础信息查询
+(instancetype)getBaseInfoCmd:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd getInfoCmd];
    task.parse =  ^NSObject *(NSData *data) {
        NSMutableDictionary *dic = [DYPrintParse infoWithData:data];
        return dic;
    };
    task.compelete = compelete;
    return task;
    
}

//查询关机时间
+(instancetype)getShutTimeCmd:(TaskCompelete)compelete{
    DYPrintTaskCmd * cmd = [[DYPrintTaskCmd alloc]init];
    cmd.data = [DYPrintCmd closeTimeCmd];
    cmd.parse =  ^NSObject *(NSData *data) {
        NSInteger time = [DYPrintParse closeTimeWithData:data];
        return time > 0 ? @(time - 1) : nil;
    };
    cmd.compelete = compelete;
    return cmd;
    
}
//查询型号
+(instancetype)getModeCmd:(TaskCompelete)compelete{
    DYPrintTaskCmd * cmd = [[DYPrintTaskCmd alloc]init];
    cmd.data = [DYPrintCmd getModelCmd];
    cmd.parse =  ^NSObject *(NSData *data) {
        NSString *model = [DYPrintParse mdStringWithData:data];
        return model;
    };
    cmd.compelete = compelete;
    return cmd;
    
}
//查询浓度
+(instancetype)getPrintColorCmd:(TaskCompelete)compelete{
    DYPrintTaskCmd * cmd = [[DYPrintTaskCmd alloc]init];
    cmd.data = [DYPrintCmd getThickCmd];
    cmd.parse =  ^NSObject *(NSData *data) {
        NSUInteger color = [DYPrintParse thickWithData:data];
        return @(color);
    };
    cmd.compelete = compelete;
    return cmd;
}
//设置关机时间
+ (instancetype)setShutTimeCmd:(NSUInteger)time compelete:(TaskCompelete)compelete {
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd setShutTimeCmd:time];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}

//设置颜色
+ (instancetype)setColorCmd:(NSUInteger)color compelete:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd setColorCmd:color];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}
//设置纸张类型
+ (instancetype)setPaperTypeCmd:(DYPrintPaperType)paper compelete:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd setPaperTypeDataCmd:paper];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}

//设置定位
+ (instancetype)setPaperPostionCmd:(DYPrintPaperType)paper compelete:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd printerLocationA4:paper];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}

+ (instancetype)returePaperCompelete:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd printerOut];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}
//进纸张
+ (instancetype)enterPaperCompelete:(TaskCompelete)compelete{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [DYPrintCmd printerEnter];
    task.parse = nil;
    task.compelete = compelete;
    return task;
}


//写入更新固件数据
+ (instancetype)sendUpdateDataTask:(NSData *)data loc:(NSUInteger)loc compelete:(TaskCompelete)compelete {
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = [self sendDataWithData:data loc:loc];
    task.parse =  ^NSObject *(NSData *data) {
        NSUInteger credit = [DYPrintParse updateCreditWithData:data];
        return credit > 0 ? @(1) : nil;
    };
    task.compelete = compelete;
    return task;
}




/// 普通指令，没有解析
/// - Parameters:
///   - compelete: 回调
///   - data: 指令数据
+ (instancetype)commentCmd:(TaskCompelete)compelete data:(NSData*)data{
    DYPrintTaskCmd *task = [[DYPrintTaskCmd alloc] init];
    task.data = data;
    task.parse = nil;
    task.compelete = compelete;
    return task;
}












//固件数据
+ (NSData *)sendDataWithData:(NSData *)fileData loc:(NSUInteger)loc {

    NSUInteger mtu = 256;
    NSUInteger unSendLen = fileData.length - loc;
    NSUInteger len = MIN(unSendLen, mtu);
    NSData *shouldSendData = [fileData subdataWithRange:NSMakeRange(loc, len)];
    NSInteger index = (loc + mtu - 1) / mtu;
    loc = loc + len;
    
    NSUInteger k = 6 + 7 + len;
    Byte k1 = (k >> 8) & 0xFF;
    Byte k2 = k & 0xFF;
    
    NSUInteger l = 7 + len;
    Byte l1 = (l >> 8) & 0xFF;
    Byte l2 = l & 0xFF;
    
    Byte index1 = (index >> 24) & 0xFF;
    Byte index2 = (index >> 16) & 0xFF;
    Byte index3 = (index >> 8) & 0xFF;
    Byte index4 = (index & 0xFF);
    
    Byte len1 = (len >> 8) & 0xFF;
    Byte len2 = len & 0xFF;
    
    Byte list[] = {0x1B, 0x10, k1, k2, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, l1, l2, 0x00, index1, index2, index3, index4, len1, len2};
    
    NSMutableData *data = [NSMutableData dataWithBytes:list length:sizeof(list)];
    [data appendData:shouldSendData];
    
    Byte all[data.length];
    [data getBytes:&all length:data.length];
    
    Byte enc = [DYPrintParse calculateEnc:all length:sizeof(all)];
    [data appendBytes:&enc length:1];
    return data;
}








@end
